<template>
  <div class="container">
    <div id="box">
      <h3>商品列表</h3>
      <el-table :data="informationForm" style="width: 100%">
        <el-table-column label="学号" prop="id" />
        <el-table-column label="姓名" prop="cname" />
        <el-table-column label="班级" prop="class" />
        <el-table-column label="专业" prop="major" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" type="danger" @click="delList(scope.$index)"
              >删除
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="modifyList(scope.$index, scope.row)"
              >修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <div id="box2">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="学号" prop="id">
        <el-input type="number" v-model="ruleForm.id" min="1"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="cname">
        <el-input type="text" v-model="ruleForm.cname"></el-input>
      </el-form-item>
      <el-form-item label="班级" prop="class">
        <el-input v-model.number="ruleForm.class"></el-input>
      </el-form-item>
      <el-form-item label="专业" prop="major">
        <el-input v-model.number="ruleForm.major"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()" :disabled="status"
          >添加</el-button
        >
        <el-button @click="preservation()">保存修改</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div style="margin-top: 15px; width: '30%'">
    <el-input placeholder="请输入学号" v-model="inp" class="inp"> </el-input>
    <el-button type="primary" @click="searchList(inp)">查询</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      status: false,
      informationForm: [],
      inp: "",
      ruleForm: {
        id: "",
        cname: "",
        class: "",
        major: "",
      },
      a: 0,
      fromData: [],
      forme: "ruleForm",
      bu: true,       
    };
  },
  created() {
    this.informationForm = JSON.parse(localStorage.getItem("fromData") || "[]");
  },
  methods: {
    add() {
      var tmp = {
        id: this.ruleForm.id,
        cname: this.ruleForm.cname,
        class: this.ruleForm.class,
        major: this.ruleForm.major,
      };
      this.informationForm.push(tmp);
      this.setloc("fromData", this.informationForm);
      this.$refs[this.forme].resetFields();
    },
    submitForm() {
      var flag = true;
      for (var i in this.ruleForm) {
        if (this.ruleForm[i] === "" || !this.ruleForm[i]) {
          flag = false;
        }
      }
      if (flag) {
        var thisIndex = this.informationForm.findIndex((obj) => {
          return this.ruleForm.id == obj.id;
        });
        if (thisIndex > -1) {
          alert("你输入的学号已存在，请检查信息");
          return false;
        } else {
          this.add();
        }
      } else {
        alert("您输入信息的不完整");
        return false;
      }
    },
    searchList(val) {
      if (!val && val == "") {
        alert("您输入的内容为空");
        return false;
      }

      var thisIndex = this.informationForm.findIndex((obj) => {
        return val == obj.id;
      });
      if (thisIndex > 0) {
        alert(
          "学号为" +
            val +
            "的学生的姓名是" +
            this.informationForm[thisIndex].cname
        );
        this.inp = "";
      } else {
        alert("未找到您查询的信息，请检查当前输入的数据");
      }
    },
    delList(index) {
      this.informationForm.splice(index, 1);
      this.setloc("fromData", this.informationForm);
    },
    modifyList(index, row) {
      var tmp = {
        id: row.id,
        cname: row.cname,
        class: row.class,
        major: row.major,
      };
      this.ruleForm = tmp;
      this.a = index;
       this.status = true;
    },
    preservation() {
      var s = confirm("确定保存修改后的数据");
      if (s) {
        var arr = this.informationForm.splice(this.a, 1, this.ruleForm);
        this.setloc("fromData", this.informationForm);
        this.informationForm =JSON.parse(localStorage.getItem("fromData")) 
         this.$refs[this.forme].resetFields();
      } else {
        alert("已取消保存");
        this.$refs[this.forme].resetFields();
             }
       this.status = false;
    },
    setloc(name, data) {
      localStorage.setItem(name, JSON.stringify(data));
    },
  },
  components: {},
};
</script>

<style>
#box {
  width: 80%;
  margin: auto;
}
#box2 {
  width: 25%;
  margin: 6% 30% 5% 20%;
}
.inp {
  width: 20%;
  display: inline-block;
  margin-left: -26%;
}
</style>
